﻿using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.ServiceModel.Configuration;
namespace DispatchingTelephonePOC
{
    public class AppConfigSave
    {
        public static void UpDataToAppKey(string key, string value)
        {
            //读取程序集的配置文件
            string assemblyConfigFile = Assembly.GetEntryAssembly().Location;

            Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
            //获取appSettings节点
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

            //删除name，然后添加新值
            appSettings.Settings.Remove(key);
            appSettings.Settings.Add(key, value);
            //保存配置文件
            config.Save();
            // 强制重新载入配置文件的ConnectionStrings配置节
            ConfigurationManager.RefreshSection("ConnectionStrings");
        }

        public static void AddToAppKey(string key, string value)
        {
            //读取程序集的配置文件
            string assemblyConfigFile = Assembly.GetEntryAssembly().Location;

            Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
            //获取appSettings节点
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

            //存在不加
            if (!appSettings.Settings.AllKeys.Contains(key))
            {
                appSettings.Settings.Add(key, value);
            }
            //保存配置文件
            config.Save();
        }
        public static string GetToAppValue(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
        ///   <summary>
        ///  读取EndpointAddress
        ///   </summary>
        ///   <param name="endpointName"></param>
        ///   <returns></returns>
        public static string GetEndpointAddress(string endpointName)
        {
            ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
            foreach (ChannelEndpointElement item in clientSection.Endpoints)
            {
                if (item.Name == endpointName)
                    return item.Address.ToString();
            }
            return string.Empty;
        }
        ///   <summary>
        ///  设置EndpointAddress
        ///   </summary>
        ///   <param name="endpointName"></param>
        ///   <param name="address"></param>
        public static void SetEndpointAddress(string endpointName, string address)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ClientSection clientSection = config.GetSection("system.serviceModel/client") as ClientSection;
            foreach (ChannelEndpointElement item in clientSection.Endpoints)
            {
                if (item.Name != endpointName)
                    continue;
                item.Address = new Uri(address);
                break;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("system.serviceModel");
        }

    }
}
